09. jQuery's Other Async Methods
jQuery has a number of other methods that can be used to make asynchronous calls. These methods are:
Each one of these functions in turn calls jQuery's main .ajax()
method. These are called "convenience methods" because they provide a convenient interface and do some default configuration of the request before calling .ajax()
.
Let's look at the .get()
and .post()
methods to see how they just call .ajax()
under the hood.
Add a Breakpoint
With the project open in a browser:
- load up DevTools
- open the Sources pane
- open the jQuery file
- add a breakpoint to line 8797
- reload the page (this will pause the code at the breakpoint you just made!)
The first time through the loop, the method
variable will be get
. This makes
jQuery[ method ] = function(...) { … }
become
jQuery[ 'get' ] = function( … ) { … }
which gives us the $.get()
method!
On line 8807 you can see that this new jQuery[ 'get' ]
function returns a call made to jQuery.ajax( … );
! Notice that before the .ajax()
call is run, the type
property is set to the method
variable (which is still'get'
). So calling $.get()
calls $.ajax()
with some preset properties.
All this was for 'get'
. This exact same code runs right after this for 'post'
! So the code creates a jQuery[ 'post' ]
function that will call jQuery.ajax( … )
and set the type
property to 'post'
.
Isn't it pretty cool how jQuery provides these convenience methods that just end up calling the main .ajax()
method!?
Which Method Should You Use?
It's often considered good practice to use the $.ajax() method over the jQuery provided convenience methods.